package com.yyc.stocktake2.ui.frg

import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.lifecycle.viewModelScope
import com.yyc.stocktake2.R
import com.yyc.stocktake2.base.BaseFragment
import com.yyc.stocktake2.bean.AppRoomDataBase
import com.yyc.stocktake2.databinding.FRfidConfigBinding
import com.yyc.stocktake2.ext.initClose
import com.yyc.stocktake2.ext.showMessage
import com.yyc.stocktake2.viewmodel.SettingModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.ext.nav

/**
 * User: Nike
 *  2024/3/20 17:08
 */
class RfidConfigFrg: BaseFragment<SettingModel, FRfidConfigBinding>() {

    val configDao = AppRoomDataBase.get().getConfigDao()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeToolbar.toolbar.initClose(getString(R.string.upload)) {nav().navigateUp()}

        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()
        mViewModel.rfid_power.set(getString(R.string.settingAntennaPower_title))
        mViewModel.rfid_power2.set(getString(R.string.other_power))

        mViewModel.viewModelScope.launch(Dispatchers.IO) {
            val bean = configDao.findFirst()
            withContext(Dispatchers.Main) {
                mDatabind.seekbar.progress = bean.rfidPower
                mDatabind.seekbar2.progress = bean.otherPower
                mDatabind.spSession.setSelection(bean.session)
                mDatabind.spMode.setSelection(bean.inventoryFlag)
            }
        }

        mDatabind.seekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener{

            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                mViewModel.rfid_power.set(getString(R.string.settingAntennaPower_title) + "：" + p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }
        })
        mDatabind.seekbar2.setOnSeekBarChangeListener(object : OnSeekBarChangeListener{

            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                mViewModel.rfid_power2.set(getString(R.string.other_power) + "：" + p1)
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {

            }

            override fun onStopTrackingTouch(p0: SeekBar?) {

            }
        })
    }

    inner class ProxyClick(){

        fun omSubmit(){
            mViewModel.viewModelScope.launch(Dispatchers.IO) {
                val bean = configDao.findFirst()
                bean.rfidPower = mDatabind.seekbar.progress
                bean.otherPower = mDatabind.seekbar2.progress
                bean.session = mDatabind.spSession.selectedItemPosition
                bean.inventoryFlag = mDatabind.spMode.selectedItemPosition
                configDao.update(bean)
                withContext(Dispatchers.Main) {
                    showMessage(message = getString(R.string.release_success))
                }
            }
        }

    }

}